IF EXISTS (SELECT *
           FROM INFORMATION_SCHEMA.ROUTINES
           WHERE ROUTINE_NAME='pAddReturnProductDetailWithBill'
           AND ROUTINE_SCHEMA='dbo'
           AND ROUTINE_TYPE='PROCEDURE'
          )
          DROP PROCEDURE dbo.pAddReturnProductDetailWithBill
GO

CREATE PROC dbo.pAddReturnProductDetailWithBill
  @vintReturnProductId     INT,
  @vintBillDetailId        INT,
  @vintAmount			   INT,
  @vbitSuccess			   BIT OUTPUT
AS
/*@@*_____________________________________________________________________

  

_____________________________________________________________________*@@*/

/*______________________ CONSTANT DECLARATIONS _________________________*/

  DECLARE
    @cstrThisProc              NVARCHAR(128),
    @cintErrGeneral            INT

  SET @cstrThisProc            = N'pAddReturnProductDetailWithBill'
  SET @cintErrGeneral          = 50000  -- General error. Used when an
                                        -- error has occurred and no other
                                        -- specific error condition is
                                        -- assigned.

/*______________________ VARIABLE DECLARATIONS _________________________*/

  DECLARE
    @intErrNo       INT,
    @strErrMsg      NVARCHAR(400),
    @intReturn      INT

/*_______________________________ INIT _________________________________*/

  -- Setting NOCOUNT ON prevents additional recordsets with the
  -- "number of records affected" from being returned, which is not
  -- handled the same by all providers, such as for OLEDB vs. ODBC.
  SET NOCOUNT ON

/*________________________ VALIDATE PARAMETERS _________________________*/
	
	DECLARE @intSalePrice		   INT
	DECLARE @intProductId		   INT
	DECLARE @datSaleTime		   DATETIME
	DECLARE @intBillId			   INT
	DECLARE	@intSaleAmount		   INT				

/*_______________________________ MAIN _________________________________*/	
	
	BEGIN TRANSACTION
	BEGIN TRY
	
	SELECT @intSalePrice = SalePrice,
		@intProductId = ProductId,
		@intBillId = BillId,
		@intSaleAmount = Amount
	FROM BillDetail
	WHERE Id = @vintBillDetailId
	
	IF @vintAmount <= @intSaleAmount BEGIN
		SELECT @datSaleTime = SaleTime
		FROM Bill
		WHERE Id = @intBillId
			
		INSERT INTO ReturnProductDetail (
			Amount,
			ReturnProductId,
			ProductId,
			SalePrice,
			SaleTime,
			BillDetailId
		) VALUES ( 
			@vintAmount,
			@vintReturnProductId,
			@intProductId,
			@intSalePrice,
			@datSaleTime,
			@vintBillDetailId
		)

		-- Increase product amount
		UPDATE Product
		SET Product.Amount = Product.Amount + @vintAmount
		FROM Product
		WHERE Product.Id = @intProductId
		
		-- Update amount of bill detail
		UPDATE BillDetail
		SET Amount = Amount - @vintAmount
		WHERE BillDetail.Id = @vintBillDetailId
		
		-- Return value 1 indicates success
		SET @vbitSuccess = 1
	END -- end if
	ELSE BEGIN
		SET @vbitSuccess = 0
	END
	
	END TRY
	BEGIN CATCH
		GOTO lblError
	END CATCH

	IF @@TRANCOUNT > 0 BEGIN
		COMMIT TRANSACTION
	END
	RETURN
/*___________________________ ERROR HANDLER ____________________________*/
lblError:
  -- An error has occurred.  On entering this section @intErrNo should
  -- already hold the error number
	
	IF @@TRANCOUNT > 0 BEGIN
		ROLLBACK TRANSACTION
	END
	
	SET @vbitSuccess = 0
	
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	
	SELECT 
		@ErrorMessage = ERROR_MESSAGE(),
		@ErrorSeverity = ERROR_SEVERITY(),
		@ErrorState = ERROR_STATE();

	-- Use RAISERROR inside the CATCH block to return error
	-- information about the original error that caused
	-- execution to jump to the CATCH block.
	RAISERROR (@ErrorMessage, -- Message text.
			   @ErrorSeverity, -- Severity.
			   1 -- State.
			   );